#include "StdAfx.h"
#include ".\sendinfo.h"

SendInfo::SendInfo(unsigned int sendno)
{
	SendNo = sendno;
}

SendInfo::~SendInfo(void)
{
	size_t j1 = packlist.size();
	for(unsigned int i=0;i<j1;i++){
		Pack * pack1 = packlist[i];
		delete pack1;
	}
	packlist.clear();
}

void SendInfo::AddSubmit(Cmpp_Submit * sub)
{
	Pack *pack = new Pack(sub->SMSerial, sub);
	packlist.push_back(pack);
}

bool SendInfo::SMSSuc(unsigned int serialno, unsigned int * sendno, bool * isfinish)
{
	*sendno = SendNo;
	*isfinish = false;
	Pack * pack = packlist[0];
	if (pack->SerialNo == serialno) {
		delete pack;
		packlist.erase(packlist.begin());
		if (packlist.size() == 0) *isfinish = true;
		return true;
	}
	return false;
}

bool SendInfo::SMSErr(unsigned int serialno, unsigned int maxtrytimes, unsigned int * sendno, bool * isfinish)
{
	*sendno = SendNo;
	*isfinish = false;
	Pack * pack = packlist[0];
	if (pack->SerialNo == serialno) {
		pack->TryTime++;
		if (pack->TryTime >= maxtrytimes) {
			*isfinish = true;
		}
		return true;
	}
	return false;
}

Cmpp_Submit * SendInfo::GetNeedSend(unsigned int retryspan)
{
	Cmpp_Submit * sub = NULL;
	CTime now = CTime::GetCurrentTime();
	CTimeSpan span(0, 0, retryspan/60, retryspan%60);
	Pack * pack = packlist[0];
	if (now - pack->SendTime >= span) {
		pack->SendTime = CTime::GetCurrentTime();
		sub = pack->submit;
	}
	return sub;
}

bool SendInfo::RestoreSubTime(unsigned int serialno)
{
	Pack * pack = packlist[0];
	if (pack->SerialNo == serialno) {
		CTimeSpan cs(10, 0, 0, 0);
		pack->SendTime = CTime::GetCurrentTime() - cs;
		return true;
	} else {
		return false;
	}
}

void SendInfo::RestoreSubTime()
{
	CTimeSpan cs(10, 0, 0, 0);
	size_t j1 = packlist.size();
	for(unsigned int i=0;i<j1;i++){
		Pack * pack1 = packlist[i];
		pack1->SendTime = CTime::GetCurrentTime() - cs;
	}
}
